package cn.bonoon.kernel.web.handlers;

import cn.bonoon.handler.impl.StandardAutoManager;
import cn.bonoon.kernel.events.OperateEvent;
import cn.bonoon.kernel.support.entities.Persistable;
import cn.bonoon.kernel.support.models.ObjectEditor;
import cn.bonoon.kernel.support.services.GenericService;


public class OperateEditHandler<E extends Persistable> extends AbstractOperateFormHandler<E> {

	public OperateEditHandler(GenericService<E> service, StandardAutoManager initializer, Class<?> editClass) throws Exception{
		super(SAVE_INSERT, service, initializer, editClass);
	}

	@Override
	protected final E internalExecute(OperateEvent event, ObjectEditor editor) {
		if(editor.isNew()){
			return service.save(event);
		}
		return service.update(event, editor.getId());
	}

	public void resetUrl(String url) {
		this.url = url;
	}
}
